home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / gemtrm12.zoo / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-08  |  14.0 KB  |  661 lines

  1. /*********************************************************************
  2.  * GEMTERM V1.2
  3.  * 1992 by Martin F. Gergeleit
  4.  * placed in the public domain
  5.  *
  6.  * GEMTERM COMES WITH ABSOLUTELY NO WARRANTY, NOR WILL I BE LIABLE FOR ANY
  7.  * DAMAGES INCURRED FROM THE USE OF IT. USE ENTIRELY AT YOUR OWN RISK!!!
  8.  *********************************************************************/
  9.  
  10. #include <gemfast.h>
  11. #include "txtwin.h"
  12. #include <basepage.h>
  13.  
  14. #ifdef DEBUG
  15. #include <stdio.h>
  16. #endif
  17.  
  18. extern int   handle;
  19. extern GRECT screen;
  20.  
  21. extern char copy_buffer[];
  22.  
  23. char *get_hist_line();
  24.  
  25. static char buffer[MAX_COLS];
  26. static short sx, sy, sc, sinv;
  27. static char *bp = 0;
  28.  
  29. static char  outstr[] = "x";
  30.  
  31. static char hist_buffer[MAX_COLS];
  32.  
  33. set_clip(x,y,w,h)
  34. int x,y,w,h;
  35. {
  36. int clip[4];
  37.  
  38. #ifdef DEBUG 
  39.   fprintf(stderr, "set_clip (%d, %d, %d, %d)\n",x,y,w,h);
  40. #endif
  41.  
  42.    clip[0]=x;
  43.    clip[1]=y;
  44.    clip[2]=x+w-1;
  45.    clip[3]=y+h-1;
  46.    vs_clip(handle,1,clip);
  47. }
  48.  
  49. txt2screen(win, tx, ty, sx, sy)
  50. txt_win *win;
  51. int tx, ty;
  52. int *sx, *sy;
  53. {
  54.   *sx = win->xwork + (tx - win->x_start) * win->c_width;
  55.   *sy = win->ywork + win->hwork - (win->y_start - ty + 1) * win->c_height;
  56.  
  57. #ifdef DEBUG 
  58.   fprintf(stderr, "txt2screen (%d, %d) = (%d, %d)\n", tx, ty, *sx, *sy);
  59. #endif
  60. }
  61.  
  62. print_str_at(win, x, y, str, inv)
  63. txt_win *win;
  64. int x, y;
  65. char *str;
  66. int inv;
  67.  
  68. {
  69. GRECT t1,t2;
  70. int temp[4];
  71.  
  72. #ifdef DEBUG 
  73.   fprintf(stderr, "print_str_at starts\n");
  74. #endif
  75.  
  76.   if (win->status != OPEN) {
  77. #ifdef DEBUG 
  78.     fprintf(stderr, "print_str_at fails (window not open)\n");
  79. #endif
  80.     return;
  81.   }
  82.  
  83.   txt2screen(win, x, y, &t2.g_x, &t2.g_y);
  84.   t2.g_w= strlen(str) * win->c_width;
  85.   t2.g_h= win->c_height;
  86.  
  87.   wind_get(win->handle,WF_FIRSTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
  88.   while (t1.g_w && t1.g_h) {
  89.    if (rc_intersect(&t2,&t1) && rc_intersect(&screen,&t1)) {
  90.      set_clip(t1.g_x,t1.g_y,t1.g_w,t1.g_h);
  91.  
  92.      if (inv) {
  93.        temp[0] = t2.g_x;
  94.        temp[1] = t2.g_y;
  95.        temp[2]=temp[0] + t2.g_w - 1;
  96.        temp[3]=temp[1] + win->c_height - 1;
  97.        v_bar(handle,temp);
  98.  
  99.        vswr_mode(handle, 4);
  100.        v_gtext(handle, t2.g_x, t2.g_y, str);
  101.        vswr_mode(handle, 1);
  102.      } else
  103.        v_gtext(handle, t2.g_x, t2.g_y, str);
  104.  
  105.    }
  106.    wind_get(win->handle,WF_NEXTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
  107.  }
  108.  
  109. #ifdef DEBUG 
  110.   fprintf(stderr, "print_str_at ends\n");
  111. #endif
  112. }
  113.  
  114. buff_print_at(win, x, y, c, inv)
  115. txt_win *win;
  116. int x, y;
  117. char c;
  118. int inv;
  119.  
  120. {
  121. /* this is to much logging in most of the cases
  122.    and what should happen in this code ? ;-)
  123. #ifdef DEBUG 
  124.   fprintf(stderr, "buff_print_at starts\n");
  125. #endif
  126. */
  127.   if ((bp != 0) && ((sy != y) || (sx+sc != x) || (sinv != inv)))
  128.     flush_buff(win);
  129.  
  130.   if (bp == 0) {
  131.     sx = x;
  132.     sy = y;
  133.     sc = 1;
  134.     sinv = inv;
  135.     bp = buffer;
  136.     *bp++ = c;
  137.     *bp = '\0';
  138. /*
  139. #ifdef DEBUG 
  140.     fprintf(stderr, "buff_print_at ends\n");
  141. #endif
  142. */
  143.     return;
  144.   }
  145.  
  146.   *bp++ = c;
  147.   *bp = '\0';
  148.   sc++;
  149.  
  150. /*
  151. #ifdef DEBUG 
  152.   fprintf(stderr, "buff_print_at ends\n");
  153. #endif
  154. */
  155. }
  156.  
  157. flush_buff (win)
  158. txt_win *win;
  159.  
  160. {
  161. #ifdef DEBUG 
  162.   fprintf(stderr, "flush_buff starts\n");
  163. #endif
  164.  
  165.   if (bp != 0) {
  166.     print_str_at(win, sx, sy, buffer, sinv);
  167.     bp = 0;
  168.   }
  169.  
  170. #ifdef DEBUG 
  171.   fprintf(stderr, "flush_buff ends\n");
  172. #endif
  173. }
  174.  
  175. clear_area(win, x1, y1, x2, y2)
  176. txt_win *win;
  177. int x1, y1, x2, y2;
  178.  
  179. {
  180. GRECT t1,t2;
  181. int temp[4];
  182.  
  183. #ifdef DEBUG 
  184.   fprintf(stderr, "clear_area starts\n");
  185. #endif
  186.  
  187.   if (win->status != OPEN) {
  188. #ifdef DEBUG 
  189.     fprintf(stderr, "clear_area fails (window not open)\n");
  190. #endif
  191.     return;
  192.   }
  193.  
  194.   flush_buff (win);
  195.  
  196.   txt2screen(win, x1, y1, &temp[0], &temp[1]);
  197.   txt2screen(win, x2, y2, &temp[2], &temp[3]);
  198.   temp[2] += win->c_width;
  199.   temp[3] += win->c_height;
  200.  
  201.   t2.g_x = temp[0];
  202.   t2.g_y = temp[1];
  203.   t2.g_w = temp[2] - temp[0];
  204.   t2.g_h = temp[3] - temp[1];
  205.  
  206.   wind_get(win->handle,WF_FIRSTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
  207.   while (t1.g_w && t1.g_h) {
  208.    if (rc_intersect(&t2,&t1) && rc_intersect(&screen,&t1)) {
  209.      set_clip(t1.g_x,t1.g_y,t1.g_w,t1.g_h);
  210.      v_bar(handle,temp);
  211.    }
  212.   wind_get(win->handle,WF_NEXTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
  213.   }
  214.  
  215. #ifdef DEBUG 
  216.   fprintf(stderr, "clear_area ends\n");
  217. #endif
  218. }
  219.  
  220. redraw_screen(win, rect)
  221. txt_win *win;
  222. GRECT *rect;
  223. {
  224. GRECT t1;
  225. int temp[4];
  226. int from, to, i;
  227. char *str;
  228. int x, y;
  229. char c;
  230.  
  231. #ifdef DEBUG 
  232.   fprintf(stderr, "redraw_screen starts\n");
  233. #endif
  234.  
  235.   if (win->status != OPEN) {
  236. #ifdef DEBUG 
  237.     fprintf(stderr, "redraw_screen fails (window not open)\n");
  238. #endif
  239.     return;
  240.   }
  241.  
  242.   from = win->lines_written - (win->hwork/win->c_height - win->y_start - 1);
  243.   to = (win->y_start) < 0 ? win->lines_written + win->y_start + 1 :
  244.                      win->lines_written;
  245.  
  246.   wind_get(win->handle,WF_FIRSTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
  247.   while (t1.g_w && t1.g_h) {
  248.     if (rc_intersect(rect,&t1) && rc_intersect(&screen,&t1)) {
  249.       set_clip(t1.g_x,t1.g_y,t1.g_w,t1.g_h);
  250.  
  251.       if (from < to)
  252.         str = get_hist_line(win, from);
  253.  
  254.       temp[0] = win->xwork;
  255.       temp[1] = win->ywork;
  256.       temp[2] = win->xwork + win->wwork - 1;
  257.       temp[3] = win->ywork + win->hwork - 1;
  258.       v_bar(handle,temp);
  259.  
  260.       for (i = from; i < to; i++) {
  261.         str = get_hist_line(win, i);
  262.         if (strlen(str) > win->x_start)
  263.           v_gtext(handle, win->xwork, win->ywork+ (i-from) * win->c_height,
  264.          &str[win->x_start]);
  265.       }
  266.  
  267.       if (win->y_start >= 0) {
  268.         temp[1] = win->ywork + (i-from) * win->c_height -1;
  269.         temp[2] = win->xwork + win->wwork - 1;
  270.         temp[3] = temp[1];
  271.         v_pline(handle, 2, temp);
  272.       }
  273.     }
  274.     wind_get(win->handle,WF_NEXTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
  275.   }
  276.  
  277.   for (y = 0; y < win->lines; y++)
  278.     for (x = 0; (c = win->wtext[y * (win->cols+1) + x]) != '\0'; x++)
  279.       buff_print_at(win, x, y, c, win->attr[y * win->cols + x]);
  280.  
  281.   flush_buff(win);
  282.   cursor(win,TRUE);
  283.  
  284. #ifdef DEBUG 
  285.   fprintf(stderr, "redraw_screen ends\n");
  286. #endif
  287. }
  288.  
  289. scroll_screen(win, lines)
  290. txt_win *win;
  291. short lines;
  292.  
  293. {
  294. int array[8];
  295. MFDB screenMFDB;
  296. GRECT t1;
  297. int tx, ty;
  298. short i;
  299. char c;
  300. char line_select[win->lines];
  301.  
  302. #ifdef DEBUG 
  303.   fprintf(stderr, "scroll_screen starts\n");
  304. #endif
  305.  
  306.   if (win->status != OPEN) {
  307. #ifdef DEBUG 
  308.     fprintf(stderr, "scroll_screen fails (window not open)\n");
  309. #endif
  310.     return;
  311.   }
  312.  
  313.   flush_buff(win);
  314.  
  315.   for (i=0; i < win->lines; i++)
  316.     line_select[i] = FALSE;
  317.  
  318.   array[0] = win->xwork;
  319.   array[1] = win->ywork + lines * win->c_height;
  320.   array[2] = win->xwork + win->wwork - 1;
  321.   array[3] = win->ywork + win->hwork - 1;
  322.   if (array[3] > screen.g_y + screen.g_h - 1)
  323.     array[3] = screen.g_y + screen.g_h - 1;
  324.   array[4] = win->xwork;
  325.   array[5] = win->ywork;
  326.   array[6] = array[2];
  327.   array[7] = array[3] - lines * win->c_height;
  328.   screenMFDB.fd_addr = 0;
  329.  
  330.   wind_get(win->handle,WF_FIRSTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
  331.   while (t1.g_w && t1.g_h) {
  332.    if (rc_intersect(&screen,&t1)) {
  333.      screen2txt(win, t1.g_x + t1.g_w - 1, t1.g_y + t1.g_h - 1, &tx, &ty);
  334.      for (i = 0; (i <= lines) && (ty - i >= 0); i++)
  335.        line_select[ty - i] = TRUE;
  336.      if ((t1.g_h -= lines * win->c_height) > 0) {
  337.        set_clip(t1.g_x,t1.g_y,t1.g_w,t1.g_h);
  338.        vro_cpyfm(handle, 3, array, &screenMFDB, &screenMFDB);
  339.      }
  340.    }
  341.   wind_get(win->handle,WF_NEXTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
  342.   }
  343.  
  344.   for (ty = 0; ty < win->lines; ty++)
  345.     if (line_select[ty]) {
  346.       for (tx = 0; (c = win->wtext[ty * (win->cols+1) + tx]) != '\0'; tx++)
  347.         buff_print_at(win, tx, ty, c, win->attr[ty * win->cols + tx]);
  348.  
  349.       flush_buff(win);
  350.       clear_area(win, tx, ty, win->cols - 1, ty);
  351.     }
  352.  
  353. #ifdef DEBUG 
  354.   fprintf(stderr, "scroll_screen ends\n");
  355. #endif
  356. }
  357.  
  358. cursor(win, on)
  359. txt_win *win;
  360. int on;
  361. {
  362. int flag;
  363.  
  364. #ifdef DEBUG 
  365.   fprintf(stderr, "cursor starts\n");
  366. #endif
  367.  
  368.   *outstr = win->wtext[win->y_cursor * (win->cols+1) + win->x_cursor];
  369.   if (*outstr >= ' ') {
  370.     flag = win->attr[win->y_cursor * win->cols + win->x_cursor];
  371.     print_str_at(win, win->x_cursor, win->y_cursor, outstr, on ? ~flag : flag);
  372.   }
  373.   else
  374.     print_str_at(win, win->x_cursor, win->y_cursor, " ", on);
  375.  
  376. #ifdef DEBUG
  377.   fprintf(stderr, "cursor ends\n");
  378. #endif
  379. }
  380.  
  381. screen2txt(win, sx, sy, tx, ty)
  382. txt_win *win;
  383. int sx, sy;
  384. int *tx, *ty;
  385. {
  386.   *tx = (sx - win->xwork) / win->c_width + win->x_start;
  387.   *ty = win->y_start - (win->ywork + win->hwork - sy) / win->c_height;
  388.  
  389. #ifdef DEBUG
  390.   fprintf(stderr, "screen2txt (%d, %d) = (%d, %d)\n", sx, sy, *tx, *ty);
  391. #endif
  392. }
  393.  
  394. put_in_hist(win, str)
  395. txt_win *win;
  396. char *str;
  397. {
  398. short del_line = FALSE;
  399.  
  400. #ifdef DEBUG
  401.   fprintf(stderr, "put_in_hist starts\n");
  402. #endif
  403.  
  404.   if(win->history == 0) {
  405. #ifdef DEBUG
  406.     fprintf(stderr, "put_in_hist ends\n");
  407. #endif
  408.     return;
  409.   }
  410.   win->last_out = win->hist_size + 1;
  411.   win->lines_written++;
  412.   do {
  413.     if (win->hist_in == win->hist_out)
  414.       del_line = TRUE;
  415.     if (del_line && (win->history[win->hist_in] == '\0'))
  416.       win->lines_written--;
  417.     win->history[win->hist_in] = *str;
  418.     win->hist_in = (win->hist_in + 1) % win->hist_size;
  419.   } while (*str++ != '\0');
  420.  
  421.   if (!del_line) {
  422. #ifdef DEBUG
  423.     fprintf(stderr, "put_in_hist ends\n");
  424. #endif
  425.     return;
  426.   }
  427.  
  428.   win->hist_out = win->hist_in;
  429.   while (win->history[win->hist_out] != '\0')
  430.     win->hist_out = (win->hist_out + 1) % win->hist_size;
  431.  
  432. #ifdef DEBUG
  433.   fprintf(stderr, "put_in_hist ends\n");
  434. #endif
  435. }
  436.  
  437. char *get_hist_line(win, number)
  438. txt_win *win;
  439. int number;
  440. {
  441. register int i;
  442. int count;
  443. char *p;
  444.  
  445. #ifdef DEBUG
  446.   fprintf(stderr, "get_hist_line starts\n");
  447. #endif
  448.  
  449.   if(win->history == 0) {
  450. #ifdef DEBUG
  451.     fprintf(stderr, "get_hist_line ends\n");
  452. #endif
  453.     return "";
  454.   }
  455.   if (number >= win->last_out) {
  456.     i = win->last_index;
  457.     count = number - win->last_out;
  458.   }
  459.   else {
  460.     i = (win->hist_out + 1) % win->hist_size;
  461.     count = number;
  462.   }
  463.  
  464.   while (count > 0) {
  465.     if (win->history[i] == '\0')
  466.       count --;
  467.     i = (i+1) % win->hist_size;
  468.   }
  469.  
  470.   win->last_out = number;
  471.   win->last_index = i;
  472.  
  473.   p = hist_buffer;
  474.   do {
  475.     *p = win->history[i];
  476.     i = (i+1) % win->hist_size;
  477.   } while (*p++ != '\0');
  478.  
  479. #ifdef DEBUG
  480.   fprintf(stderr, "get_hist_line ends\n");
  481. #endif
  482.   return hist_buffer;
  483. }
  484.  
  485. update_slider(win)
  486. txt_win *win;
  487. {
  488. int current_lines, current_cols;
  489. int tmp;
  490.  
  491. #ifdef DEBUG
  492.   fprintf(stderr, "update_slider starts\n");
  493. #endif
  494.  
  495.   if (win->status != OPEN) {
  496. #ifdef DEBUG
  497.     fprintf(stderr, "update_slider fails (window not open)\n");
  498. #endif
  499.     return;
  500.   }
  501.  
  502.   current_cols = win->wwork/win->c_width;
  503.   current_lines = win->hwork/win->c_height;
  504.  
  505.   tmp = current_cols*1000/win->cols;
  506.   if (win->hs_s != tmp) {
  507.     win->hs_s = tmp;
  508.     wind_set(win->handle, WF_HSLSIZE,tmp,0,0,0);
  509.   }
  510.  
  511.   tmp = (current_cols == win->cols) ? 1 : (win->x_start*999)/(win->cols-current_cols)+1;
  512.   if (win->hs_p != tmp) {
  513.     win->hs_p = tmp;
  514.     wind_set(win->handle, WF_HSLIDE,tmp,0,0,0);
  515.   }
  516.  
  517.   tmp = (current_lines)*999/(win->lines+win->lines_written) + 1;
  518.   if (win->vs_s != tmp) {
  519.     win->vs_s = tmp;
  520.     wind_set(win->handle, WF_VSLSIZE,tmp,0,0,0);
  521.   }
  522.  
  523.   tmp = (win->lines_written + win->lines - current_lines == 0) ? 1000:
  524.    (win->lines_written - current_lines + win->y_start + 1) * 999
  525.        / (win->lines_written + win->lines - current_lines)+1;
  526.   if (win->vs_p != tmp) {
  527.     win->vs_p = tmp;
  528.     wind_set(win->handle, WF_VSLIDE,tmp,0,0,0);
  529.   }
  530. #ifdef DEBUG
  531.   fprintf(stderr, "update_slider ends\n");
  532. #endif
  533. }
  534.  
  535. char *get_line(win, n)
  536. txt_win *win;
  537. int n;
  538. {
  539. int i;
  540.  
  541. #ifdef DEBUG
  542.   fprintf(stderr, "get_line starts\n");
  543. #endif
  544.  
  545.   for (i = 0; i < MAX_COLS; i++)
  546.     buffer[i] = '\0';
  547.  
  548.   if (n < 0)
  549.     strcpy(buffer, get_hist_line(win, win->lines_written + n));
  550.   else
  551.     strcpy(buffer, &win->wtext[n * (win->cols+1)]);
  552.  
  553. #ifdef DEBUG
  554.   fprintf(stderr, "get_line ends\n");
  555. #endif
  556.   return buffer;
  557. }
  558.  
  559. copy_to_scrapbuf(win, tx1, ty1, tx2, ty2)
  560. txt_win *win;
  561. int tx1, ty1, tx2, ty2;
  562. {
  563. char *str;
  564. int i;
  565.  
  566. #ifdef DEBUG
  567.   fprintf(stderr, "copy_to_scrapbuf starts\n");
  568. #endif
  569.  
  570.   if (ty1 > ty2) {
  571.     i = ty1;
  572.     ty1 = ty2;
  573.     ty2 = i;
  574.   }
  575.   if (tx1 > tx2) {
  576.     i = tx1;
  577.     tx1 = tx2;
  578.     tx2 = i;
  579.   }
  580.  
  581.   str = get_line(win, ty1);
  582.   strcpy(copy_buffer, &str[tx1]);
  583.  
  584.   if (ty1 == ty2) {
  585.     copy_buffer[(tx2-tx1) +1] = '\0';
  586.     if (strlen(str) <= tx2)
  587.       strcat(copy_buffer, "\n");
  588. #ifdef DEBUG
  589.     fprintf(stderr, "copy_to_scrapbuf ends\n");
  590. #endif
  591.     return;
  592.   }
  593.   
  594.   ty1++;
  595.   while (ty1 < ty2) {
  596.     strcat(copy_buffer, "\n");
  597.     str = get_line(win, ty1);
  598.     if ((strlen(str) + strlen(copy_buffer)) < (MAXCOPYLEN - 2))
  599.       strcat(copy_buffer, str);
  600.     else {
  601. #ifdef DEBUG
  602.       fprintf(stderr, "copy_to_scrapbuf ends\n");
  603. #endif
  604.       return;
  605.     }
  606.     ty1++;
  607.   }
  608.  
  609.   strcat(copy_buffer, "\n");
  610.   str = get_line(win, ty2);
  611.   if (strlen(str) <= tx2) {
  612.     if ((strlen(str) + strlen(copy_buffer)) < (MAXCOPYLEN - 2)) {
  613.       strcat(copy_buffer, str);
  614.       strcat(copy_buffer, "\n");
  615.     };
  616.   }
  617.   else {
  618.     str[tx2+1] = '\0';
  619.     if ((strlen(str) + strlen(copy_buffer)) < (MAXCOPYLEN - 1))
  620.       strcat(copy_buffer, str);
  621.   }
  622. #ifdef DEBUG
  623.   fprintf(stderr, "copy_to_scrapbuf ends\n");
  624. #endif
  625. }
  626.  
  627. char *my_getenv(varname)
  628. char *varname;
  629. {
  630. char *var;
  631. int  len;
  632.  
  633. #ifdef DEBUG
  634.   fprintf(stderr, "my_getenv starts (%s)\n", varname);
  635. #endif
  636.  
  637.   len = strlen(varname);
  638.   if (_base->p_env == 0) {
  639. #ifdef DEBUG
  640.     fprintf(stderr, "my_getenv returns 0\n");
  641. #endif
  642.     return 0;
  643.   }
  644.  
  645.   for (var = _base->p_env; *var != '\0'; var++) {
  646.     if (!strncmp(var, varname, len) && var[len] == '=') {
  647. #ifdef DEBUG
  648.       fprintf(stderr, "my_getenv returns %s\n", var+len+1);
  649. #endif
  650.       return var+len+1;
  651.     }
  652.     while (*var != '\0')
  653.       var++;
  654.   }
  655. #ifdef DEBUG
  656.   fprintf(stderr, "my_getenv returns 0\n");
  657. #endif
  658.   return 0;
  659. }
  660.  
  661.